// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <pollux/core/expressions.h>
#include <pollux/testing/vector/vector_test_base.h>

#include <gtest/gtest.h>

namespace kumo::pollux::core::test {

class TypedExprSerDeTest : public testing::Test,
                           public pollux::VectorBuilder {
 protected:
  static void SetUpTestCase() {
    memory::MemoryManager::testingSetInstance({});
  }

  TypedExprSerDeTest() {
    Type::registerSerDe();

    ITypedExpr::registerSerDe();
  }

  void testSerde(const TypedExprPtr& expression) {
    auto serialized = expression->serialize();

    auto copy =
        pollux::ISerializable::deserialize<ITypedExpr>(serialized, pool());

    ASSERT_EQ(expression->toString(), copy->toString());
    ASSERT_EQ(*expression->type(), *copy->type());
    ASSERT_EQ(*expression, *copy);
  }

  std::vector<RowVectorPtr> data_;
};

TEST_F(TypedExprSerDeTest, input) {
  auto expression = std::make_shared<InputTypedExpr>(
      ROW({"a", "b", "c"}, {BIGINT(), BOOLEAN(), VARCHAR()}));

  testSerde(expression);
}

TEST_F(TypedExprSerDeTest, fieldAccess) {
  std::shared_ptr<ITypedExpr> expression =
      std::make_shared<FieldAccessTypedExpr>(BIGINT(), "a");
  testSerde(expression);
  ASSERT_EQ(expression->toString(), "\"a\"");

  expression = std::make_shared<DereferenceTypedExpr>(
      VARCHAR(),
      std::make_shared<FieldAccessTypedExpr>(
          ROW({"a", "b"}, {VARCHAR(), BOOLEAN()}), "ab"),
      0);
  testSerde(expression);
  ASSERT_EQ(expression->toString(), "\"ab\"[a]");
}

TEST_F(TypedExprSerDeTest, constant) {
  auto expression = std::make_shared<ConstantTypedExpr>(BIGINT(), 127LL);
  testSerde(expression);

  expression =
      std::make_shared<ConstantTypedExpr>(make_flat_vector<int32_t>({123, 234}));
  testSerde(expression);

  expression = std::make_shared<ConstantTypedExpr>(make_array_vector<int64_t>({
      {1, 2, 3, 4, 5},
  }));
  testSerde(expression);
}

TEST_F(TypedExprSerDeTest, call) {
  // a + b
  auto expression = std::make_shared<CallTypedExpr>(
      BIGINT(),
      std::vector<TypedExprPtr>{
          std::make_shared<FieldAccessTypedExpr>(BIGINT(), "a"),
          std::make_shared<FieldAccessTypedExpr>(BIGINT(), "b"),
      },
      "plus");

  testSerde(expression);

  // f(g(h(a, b), c))
  expression = std::make_shared<CallTypedExpr>(
      VARCHAR(),
      std::vector<TypedExprPtr>{
          std::make_shared<CallTypedExpr>(
              DOUBLE(),
              std::vector<TypedExprPtr>{
                  std::make_shared<CallTypedExpr>(
                      BIGINT(),
                      std::vector<TypedExprPtr>{
                          std::make_shared<FieldAccessTypedExpr>(BIGINT(), "a"),
                          std::make_shared<FieldAccessTypedExpr>(BIGINT(), "b"),
                      },
                      "h"),
                  std::make_shared<FieldAccessTypedExpr>(BIGINT(), "c"),
              },
              "g"),
      },
      "f");
  testSerde(expression);
}

TEST_F(TypedExprSerDeTest, cast) {
  auto expression = std::make_shared<CastTypedExpr>(
      BIGINT(), std::make_shared<FieldAccessTypedExpr>(VARCHAR(), "a"), false);
  testSerde(expression);

  expression = std::make_shared<CastTypedExpr>(
      VARCHAR(), std::make_shared<FieldAccessTypedExpr>(BIGINT(), "a"), true);
  testSerde(expression);
}

TEST_F(TypedExprSerDeTest, concat) {
  auto expression = std::make_shared<ConcatTypedExpr>(
      std::vector<std::string>{"x", "y", "z"},
      std::vector<TypedExprPtr>{
          std::make_shared<FieldAccessTypedExpr>(BIGINT(), "a"),
          std::make_shared<FieldAccessTypedExpr>(DOUBLE(), "b"),
          std::make_shared<FieldAccessTypedExpr>(VARBINARY(), "c"),
      });
  testSerde(expression);
}

TEST_F(TypedExprSerDeTest, lambda) {
  // x -> (x in (1, ..., 5))
  auto expression = std::make_shared<LambdaTypedExpr>(
      ROW({"x"}, {BIGINT()}),
      std::make_shared<CallTypedExpr>(
          BOOLEAN(),
          std::vector<TypedExprPtr>{
              std::make_shared<FieldAccessTypedExpr>(BIGINT(), "x"),
              std::make_shared<ConstantTypedExpr>(make_array_vector<int64_t>({
                  {1, 2, 3, 4, 5},
              })),
          },
          "in"));
  testSerde(expression);
}

} // namespace kumo::pollux::core::test
